// 复制自 code 项目，保留最小绘制能力
const echarts = {
  version: '1.0.0-miniprogram',
  init: function(canvas, theme, opts){
    opts = opts || {}
    return new EChartsInstance(canvas, opts)
  }
}

function EChartsInstance(canvas, opts){
  this._dom = canvas
  this._width = opts.width || (canvas ? canvas.width : 300)
  this._height = opts.height || (canvas ? canvas.height : 150)
  this._dpr = opts.devicePixelRatio || 1
  this._option = null
  if (canvas && canvas.getContext) { this._ctx = canvas.getContext('2d') }
}

EChartsInstance.prototype = {
  constructor: EChartsInstance,
  setOption: function(option){ this._option = option; this._render(); return this },
  getOption: function(){ return this._option },
  resize: function(opts){ if (opts){ this._width = opts.width||this._width; this._height = opts.height||this._height } this._render(); return this },
  dispose: function(){ this._dom = null; this._ctx = null; this._option = null; },
  getZr: function(){ return { handler: { dispatch: function(){ } } } },
  _render: function(){
    if (!this._option || !this._ctx) return
    const ctx = this._ctx
    ctx.clearRect(0,0,this._width,this._height)
    const series = (this._option.series&&this._option.series[0]) || {}
    if (series.type === 'bar') this._renderBarChart(this._option)
    else if (series.type === 'pie') this._renderPieChart(this._option)
    else this._renderPlaceholder('')
    if (ctx.draw) ctx.draw(true)
  },
  _renderPlaceholder: function(){ const c=this._ctx; c.fillStyle='#f9fafb'; c.fillRect(0,0,this._width,this._height); },
  _renderBarChart: function(option){
    const ctx=this._ctx; const data=option.series[0].data||[]; const cats=(option.xAxis&&option.xAxis.data)||[]; if(!data.length){this._renderPlaceholder();return}
    const m={top:30,right:20,bottom:40,left:20}; const cw=this._width-m.left-m.right; const ch=this._height-m.top-m.bottom; const max=Math.max(...data)
    ctx.fillStyle='#fff'; ctx.fillRect(0,0,this._width,this._height)
    const bw=cw/data.length*0.7; const bs=cw/data.length
    data.forEach((v,i)=>{ const bh=(v/max)*ch; const x=m.left+i*bs+(bs-bw)/2; const y=m.top+ch-bh; const g=ctx.createLinearGradient(0,y,0,y+bh); g.addColorStop(0,'#3b82f6'); g.addColorStop(1,'#1d4ed8'); ctx.fillStyle=g; ctx.fillRect(x,y,bw,bh); ctx.fillStyle='#374151'; ctx.font='bold 12px Arial'; ctx.textAlign='center'; ctx.fillText(String(v), x+bw/2, y-8); if(cats[i]){ ctx.fillStyle='#6b7280'; ctx.font='11px Arial'; ctx.fillText(cats[i], x+bw/2, this._height-10) } })
  },
  _renderPieChart: function(option){
    const ctx=this._ctx; const data=option.series[0].data||[]; if(!data.length){this._renderPlaceholder();return}
    const total=data.reduce((s,i)=>s+(i.value||0),0)||1; ctx.fillStyle='#fff'; ctx.fillRect(0,0,this._width,this._height)
    const cx=this._width/2, cy=this._height/2, r=Math.min(this._width,this._height)/2.5; let a=-Math.PI/2; const colors=['#3b82f6','#10b981','#f59e0b','#ef4444','#8b5cf6']
    data.forEach((it,idx)=>{ const ang=(it.value/total)*2*Math.PI; ctx.beginPath(); ctx.moveTo(cx,cy); ctx.arc(cx,cy,r,a,a+ang); ctx.closePath(); ctx.fillStyle=colors[idx%colors.length]; ctx.fill(); a+=ang })
  }
}

module.exports = echarts

